<?php

namespace App\Http\Controllers;

use App\Models\Alarm;
use App\Models\ClusterReport;
use App\Models\ESCluster;
use App\Models\ESClusterChargeLog;
use App\Models\ESUnit;
use App\Models\ReportCar;
use Illuminate\Http\Request;
use Monolog\Handler\SyslogUdp\UdpSocket;
use PhpClickHouseLaravel\RawColumn;

class RealTimeController extends Controller
{
    public function power_detail(Request $request)
    {
        $e_s_cluster_id = $request->input('e_s_cluster_id', '10');
        $data = ESClusterChargeLog::where('e_s_cluster_id', $e_s_cluster_id)->get();
        return $data;
    }

    public function power_info(Request $request)
    {
        $e_s_cluster_id = $request->input('e_s_cluster_id', '10');
        $log = ESClusterChargeLog::where('e_s_cluster_id', $e_s_cluster_id)->get();

        $data['available_in'] = 363.8;
        $data['available_out'] = 156.2;
        $data['today_charge'] = round($log->where('state', 'charge')->where('begin_at', '>=', date('Y-m-d'))->pluck('power')->sum(), 2);
        $data['today_discharge'] = round($log->where('state', 'discharge')->where('begin_at', '>=', date('Y-m-d'))->pluck('power')->sum(), 2);
        $data['total_charge'] = round($log->where('state', 'charge')->pluck('power')->sum(), 2);
        $data['total_discharge'] = round($log->where('state', 'discharge')->pluck('power')->sum(), 2);
        $data['natural_discharge_rate'] = 10;
        $data['Charge_discharge_energy_maxdiff'] = 234.3;
        return $data;
    }

    public function unit_info(Request $request)
    {
        $e_s_cluster_id = $request->input('e_s_cluster_id', '10');
        $e_s_cluster = ESCluster::find($e_s_cluster_id);
        $data = ESUnit::find($e_s_cluster->e_s_unit_id);
        return $data;
    }

    public function history_info(Request $request)
    {
        $e_s_cluster_id = $request->input('e_s_cluster_id', '10');
        $reports = ClusterReport::where('e_s_cluster_id', $e_s_cluster_id)->orderByDesc('created_at')->get();
    
        foreach ($reports as &$item) {
            $item['power'] =  round($item['current'] * $item['voltage'], 2);
        }
        
        return $reports->setVisible(['SOH', 'power', 'created_at']);
    }

    public function alarm_info(Request $request)
    {
        $e_s_cluster_id = $request->input('e_s_cluster_id', '10');
        $alarm = Alarm::where('e_s_cluster_id', $e_s_cluster_id)->orderByDesc('created_at')->pluck('name');
        $data = $alarm->countBy();
        return $data;
    }

    public function alarm_detail(Request $request)
    {
        $e_s_cluster_id = $request->input('e_s_cluster_id', '10');
        $alarm = Alarm::where('e_s_cluster_id', $e_s_cluster_id)->orderByDesc('created_at')->get();
        return $alarm;
    }

    public function show(Request $request)
    {
        $e_s_cluster_id = $request->input('e_s_cluster_id', '10');
        $reports = ClusterReport::where('e_s_cluster_id', $e_s_cluster_id)->orderByDesc('created_at');
    
        $last = $reports->first();
        $last['max_temperature'] = 37;
        $last['min_temperature'] = 15;
        $last['max_voltage'] = 780;
        $last['min_voltage'] = 200;
        $last['power'] = round($last['current'] * $last['voltage'], 2);
        
        $data['current_info'] = $last;

        $data['power_detail'] =  $this->power_detail($request);
        $data['power_info'] =  $this->power_info($request);
        $data['clusters_info'] =  $this->unit_info($request);
        $data['history_info'] =  $this->history_info($request);
        $data['alarm_info'] =  $this->alarm_info($request);
        $data['alarm_detail'] =  $this->alarm_detail($request);

        return $this->respondSuccess($data);
    }
}
